package com.lagou.edu.listener;

import com.lagou.edu.factory.BeanFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 自定监听器，监听容器启动事件，从中获取到配置的basePackage信息，以便完成初始化工作
 */
public class MyContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("容器启动..");
        String basePackage = servletContextEvent.getServletContext().getInitParameter("basePackage");
        System.out.println("basePackage=" + basePackage);
        try {
            BeanFactory.init(basePackage);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
